package cn.edu.xmu.oomall.comment.controller;

import cn.edu.xmu.javaee.core.aop.Audit;
import cn.edu.xmu.javaee.core.aop.LoginUser;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.javaee.core.util.CloneFactory;
import cn.edu.xmu.oomall.comment.controller.dto.CommentDto;
import cn.edu.xmu.oomall.comment.controller.vo.CommentIdNameTypeVo;
import cn.edu.xmu.oomall.comment.dao.bo.Comment;
import cn.edu.xmu.oomall.comment.service.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController /*Restful的Controller对象*/
@RequiredArgsConstructor
@RequestMapping(produces = "application/json;charset=UTF-8")
public class CommentController {

    private final CommentService commentservice;

    /**
     * 创建评论
     * @param orderItemId 订单Id
     * @param user 登录用户
     * @param dto 评论数据
     * @return
     */
    @PostMapping("/oredritems/{oid}/comments")
    @Audit(departName = "comments")
    public ReturnObject createComment(@PathVariable(value = "oid") Long orderItemId,
                                      @LoginUser UserDto user,
                                      @RequestBody CommentDto dto) {
        Comment comment = CloneFactory.copy(new Comment(), dto);
        Comment newComment = this.commentservice.createComment(orderItemId, comment, user);
        CommentIdNameTypeVo vo = CommentIdNameTypeVo.builder().id(newComment.getId()).content(newComment.getContent()).build();
        return new ReturnObject(ReturnNo.CREATED,vo);
    }

    /**
     * 商家回复评论
     * @param id
     * @param shopId
     * @param user
     * @param dto
     * @return
     */
    @PostMapping("/shops/{shopId}/comments/{commentId}/reply" )
    @Audit(departName = "comments")
    public ReturnObject createReply(@PathVariable(value = "commentId") Long id,@PathVariable Long shopId, @LoginUser UserDto user, @RequestBody CommentDto dto) {
        Comment comment = CloneFactory.copy(new Comment(), dto);
        Comment ReplyComment=this.commentservice.createReply(id,shopId, comment, user);
        CommentIdNameTypeVo vo = CommentIdNameTypeVo.builder()
                .id(ReplyComment.getId())
                .content(ReplyComment.getContent())
                .build();
        return new ReturnObject(ReturnNo.CREATED,vo);
    }
    /**
     * 追评
     * @param id 评论id
     * @param user 登录用户
     * @param dto 评论数据
     * @return
     */
    @PostMapping("/comments/{cid}/append" )
    @Audit(departName = "comments")
    public ReturnObject addAdditionalComment(@PathVariable(value = "cid") Long id, @LoginUser UserDto user, @RequestBody CommentDto dto) {
        Comment comment = CloneFactory.copy(new Comment(), dto);
        Comment addtionalComment=this.commentservice.addAdditionalComment(id, comment, user);
        CommentIdNameTypeVo vo = CommentIdNameTypeVo.builder()
                .id(addtionalComment.getId())
                .content(addtionalComment.getContent())
                .build();
        return new ReturnObject(ReturnNo.CREATED,vo);
    }


}